package com.consumer1.util;


import cn.hutool.core.io.FileUtil;
import com.consumer1.entity.ImportMovie;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.PutObjectRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @version v1.0
 * @author: TianXiang
 * @description:
 * @date: 2020/10/25
 */
@Component
public class FileUtils {
    private static final Logger LOG = LoggerFactory.getLogger(FileUtils.class.getName());

    @Value("${cos.bucketName}")
    private String bucketName;

    @Autowired
    private COSClient cosClient;

    public void transferM3U8(ImportMovie importMovie) {
        FFmpegUtils.transferTS(importMovie);
        // 转完m3u8格式后删除源文件
        this.deleteTempFile(importMovie);
        // 二级目录-获取目录下所有的音频文件
        String separator;
        int index;// 截取到html的目录
        if(SysUtils.isLinux()) {
            // /usr/local/app/docker/nginx/data/html/movies/20210323/50db02a1/temp.mp4
            separator = "/";
            index = 33;
        }else {
            separator = "\\\\";
            index = 3;
        }
        String filePath = importMovie.getMovieFilePath();
        String secondFilePath = filePath.substring(0, filePath.lastIndexOf(separator.indexOf("\\\\") != -1 ? "\\" : separator));
        File secondFile = new File(secondFilePath);
        File[] files = secondFile.listFiles();
        for (int i = 0; i < files.length; i++) {
            File tsFile = files[i];
            // 指定文件上传到 COS 上的路径，即对象键。例如对象键为folder/picture.jpg，则表示将文件 picture.jpg 上传到 folder 路径下
            String key;
            if(-1 != secondFilePath.indexOf("greens")) {
                // green_movie/日期/uuid/
                String keyPre = secondFilePath.substring(secondFilePath.indexOf(separator.indexOf("\\\\") != -1 ? "\\" : separator, index) + 1).replace("greens", "green_movie").replaceAll(separator, "/");
                key = keyPre + "/"+ tsFile.getName();
            }else {
                // movie/日期/uuid/
                String keyPre = secondFilePath.substring(secondFilePath.indexOf(separator.indexOf("\\\\") != -1 ? "\\" : separator, index) + 1).replace("movies", "movie").replaceAll(separator, "/");
                key = keyPre + "/" + tsFile.getName();
            }

            // 上传到腾讯云COS对象云
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, tsFile);
            cosClient.putObject(putObjectRequest);
        }
        // 删除整个二级目录
        FileUtil.del(secondFile);
    }

    /**
     * 删除本地临时文件
     */
    public void deleteTempFile(ImportMovie importMovie) {
        // temp.mp4
        File mp4File = new File(importMovie.getMovieFilePath());
        if (mp4File != null) {
            new File(mp4File.toURI()).delete();

            // temp.ts
            File tsFile = new File(importMovie.getUuidDirPath() + File.separator + "temp.ts");
            if (tsFile != null) {
                new File(tsFile.toURI()).delete();
            }
        }
    }
}
